package com.itheima.fengkuanglianxi.bookmanager.bookcontroller;

import com.itheima.fengkuanglianxi.bookmanager.bookservice.BookService;
import com.itheima.fengkuanglianxi.bookmanager.domain.Book;

import java.util.Scanner;

public abstract class BaseBookController {
    private static BookService bookService = new BookService();

    public void start() {

        while(true){

            printView();

            userSelect();

        }

    }

    public abstract void printView();

    public void userSelect(){
        System.out.println("请输入您的选择：");

        Scanner sc = new Scanner(System.in);
        String next = sc.next();
        switch (next){
            case "1" :
                addBook();
                break;
            case "2" :
                deleteBook();
                break;
            case "3" :
                updateBook();
                break;
            case "4" :
                getAllBooks();
                break;
            case "5":
                System.out.println("欢迎下次使用");
                System.exit(0);
                break;
            default:
                System.out.println("您的选择有误，请重新选择");
                break;

        }
    }


    public void getAllBooks() {

        System.out.println("图书id\t\t图书名称\t\t发布时间\t\t图书价格");

        Book[] books = bookService.getAllBooks();

        for (int i = 0; i < books.length; i++) {
            Book bookArray = books[i];
            if(bookArray != null){
                System.out.println(bookArray.getBookId() + "\t\t" + bookArray.getBookName() + "\t\t" + bookArray.getPublishDate() + "\t\t" + bookArray.getPrice());
            }
        }

    }

    public void updateBook() {
        while(true){
            System.out.println("请输入要修改的图书id：");
            Scanner sc = new Scanner( System.in);
            String bookId = sc.next();

            boolean b = bookService.isExitsBook(bookId);

            if(b){
                System.out.println("请输入新的图书名称：");
                String bookName = sc.next();
                System.out.println("请输入新图书的发布时间：");
                String publishDate = sc.next();
                System.out.println("请输入新图书的价格");
                String price = sc.next();

                Book book = new Book();
                book.setBookId(bookId);
                book.setBookName(bookName);
                book.setPublishDate(publishDate);
                book.setPrice(price);

                bookService.updateBook(book);

                System.out.println("图书信息修改成功");
                break;

            }else{
                System.out.println("您输入的图书id不存在，请重新输入");
            }
        }

    }

    public void deleteBook() {

        while (true){
            System.out.println("请输入要删除的图书id：");
            Scanner sc = new Scanner(System.in);
            String bookId = sc.next();

            boolean b = bookService.isExitsBook(bookId);
            if(b){
                bookService.deleteBook(bookId);
                System.out.println("删除图书成功");
                break;
            }else{
                System.out.println("您输入的图书id不存在，请重新输入");
            }
        }
    }

    public void addBook() {

        while(true){
            System.out.println("请输入要添加的图书id：");
            Scanner sc = new Scanner(System.in);
            String bookId = sc.next();

            boolean b = bookService.isExitsBook(bookId);

            if(b){
                System.out.println("该图书id已存在，请重新输入");
            }else{
                System.out.println("请输入图书名称：");
                String bookName = sc.next();

                System.out.println("请输入图书发布日期：");
                String publishDate = sc.next();

                System.out.println("请输入图书价格：");
                String price = sc.next();

                Book book = new Book();
                book.setBookId(bookId);
                book.setBookName(bookName);
                book.setPublishDate(publishDate);
                book.setPrice(price);

                boolean c = bookService.addBook(book);

                if(c){
                    System.out.println("添加图书成功");
                    break;
                }else{
                    System.out.println("添加图书失败");
                }

            }

        }
    }
}
